package cn.edu.huas.shopping.utils;

import cn.edu.huas.shopping.exception.DaoException;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Objects;
import java.util.Properties;

public class DBUtils {
    private static String driverClass;
    private static String username;
    private static String password;
    private static String url;

    //执行更新
    public static void update(String sql,Object...args) throws SQLException {
        Connection  conn=null;
        PreparedStatement ps=null;
        try {
            conn=DBUtils.newConnection();
            ps=conn.prepareStatement(sql);
            if(Objects.nonNull(args)&&args.length>0){
                for (int i=0;i< args.length;i++){
                    ps.setObject(i+1,args[i]);
                }
            }
            ps.executeUpdate();
        } catch (SQLException e) {
            throw new DaoException(e);
        }finally {
            DBUtils.close(conn,ps);
        }
    }


   /* //根据用户名和密码执行查询功能
    public User queryByUserNameAndPwd(String username,String pwd) throws DaoException{
        Connection  conn=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        String sql="select id,username,dotime from t_user where username=? and pwd=?";
        try {
            conn=DBUtils.newConnection();
            ps=conn.prepareStatement(sql);
            ps.setString(1,username);
            ps.setString(2,pwd);
            //执行sql语句
            rs=ps.executeQuery();
            User u=null;
            if (rs.next()){
                u=new User();
                Integer id=rs.getInt("id");
                String name=rs.getString("username");
                Timestamp timestamp=rs.getTimestamp("dotime");
                u.setId(id);
                u.setUsername(name);
                u.setDoTime(timestamp.toLocalDateTime());
            }
            return u;
        } catch (SQLException e) {
            throw new DaoException(e);
        } finally {
            DBUtils.close(conn,ps);
        }
    }*/


    //加载驱动
    static {
        try {
            Properties props=new Properties();
            InputStream is=DBUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");
            props.load(is);
            driverClass= props.getProperty("jdbc.driverClassName").trim();
            username= props.getProperty("jdbc.username");
            password=props.getProperty("jdbc.password");
            url= props.getProperty("jdbc.url");

            //加载驱动
            Class.forName(driverClass);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static Connection newConnection(){
        try {
            return DriverManager.getConnection(url,username,password);
        } catch (SQLException e) {
            throw new DaoException(e);
        }
    }


    //关闭资源
    public static void close(Connection conn){
        try {
            if (Objects.nonNull(conn)) {
                conn.close();
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    //关闭资源
    public static void close(Connection conn, Statement stm){
        try {
            if (Objects.nonNull(conn)) {
                conn.close();
            }
            if (Objects.nonNull(conn)){
                conn.close();
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    //查询
    public static<T>T query(String sql,RowMapper<T> rowMapper,Object...args){
        Connection  conn=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try {
            conn=DBUtils.newConnection();
            ps=conn.prepareStatement(sql);

            //执行sql语句
            if(Objects.nonNull(args)&&args.length>0){
                for (int i=0;i< args.length;i++){
                    ps.setObject(i+1,args[i]);
                }
            }
            //执行sql语句
            rs=ps.executeQuery();
            //定义了一个接口，该接口解析
            return rowMapper.parseResultSet(rs);
        } catch (SQLException e) {
            throw new DaoException(e);
        } finally {
            DBUtils.close(conn,ps);
        }
    }
}
